<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="路线名称" prop="routeName">
      <el-input v-model="dataForm.routeName" placeholder="路线名称"></el-input>
    </el-form-item>
    <el-form-item label="描述" prop="routeDescribe">
      <el-input v-model="dataForm.routeDescribe" placeholder="描述"></el-input>
    </el-form-item>
    <el-form-item label="阅读人数" prop="peopleNum">
      <el-input v-model="dataForm.peopleNum" placeholder="阅读人数"></el-input>
    </el-form-item>
    <el-form-item label="课程时长" prop="classHours">
      <el-input v-model="dataForm.classHours" placeholder="课程时长"></el-input>
    </el-form-item>
    <el-form-item label="课程数量" prop="classNum">
      <el-input v-model="dataForm.classNum" placeholder="课程数量"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="sort">
      <el-input v-model="dataForm.sort" placeholder="排序"></el-input>
    </el-form-item>
    <el-form-item label="状态  0：禁用   1：正常" prop="isStatus">
      <el-input v-model="dataForm.isStatus" placeholder="状态  0：禁用   1：正常"></el-input>
    </el-form-item>
    <el-form-item label="注册时间" prop="gmtCreate">
      <el-input v-model="dataForm.gmtCreate" placeholder="注册时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="gmtModified">
      <el-input v-model="dataForm.gmtModified" placeholder="更新时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          routeId: 0,
          routeName: '',
          routeDescribe: '',
          peopleNum: '',
          classHours: '',
          classNum: '',
          sort: '',
          isStatus: '',
          gmtCreate: '',
          gmtModified: ''
        },
        dataRule: {
          routeName: [
            { required: true, message: '路线名称不能为空', trigger: 'blur' }
          ],
          routeDescribe: [
            { required: true, message: '描述不能为空', trigger: 'blur' }
          ],
          peopleNum: [
            { required: true, message: '阅读人数不能为空', trigger: 'blur' }
          ],
          classHours: [
            { required: true, message: '课程时长不能为空', trigger: 'blur' }
          ],
          classNum: [
            { required: true, message: '课程数量不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ],
          isStatus: [
            { required: true, message: '状态  0：禁用   1：正常不能为空', trigger: 'blur' }
          ],
          gmtCreate: [
            { required: true, message: '注册时间不能为空', trigger: 'blur' }
          ],
          gmtModified: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.routeId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.routeId) {
            this.$http({
              url: this.$http.adornUrl(`/ly/studyroute/info/${this.dataForm.routeId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.routeName = data.studyRoute.routeName
                this.dataForm.routeDescribe = data.studyRoute.routeDescribe
                this.dataForm.peopleNum = data.studyRoute.peopleNum
                this.dataForm.classHours = data.studyRoute.classHours
                this.dataForm.classNum = data.studyRoute.classNum
                this.dataForm.sort = data.studyRoute.sort
                this.dataForm.isStatus = data.studyRoute.isStatus
                this.dataForm.gmtCreate = data.studyRoute.gmtCreate
                this.dataForm.gmtModified = data.studyRoute.gmtModified
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/ly/studyroute/${!this.dataForm.routeId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'routeId': this.dataForm.routeId || undefined,
                'routeName': this.dataForm.routeName,
                'routeDescribe': this.dataForm.routeDescribe,
                'peopleNum': this.dataForm.peopleNum,
                'classHours': this.dataForm.classHours,
                'classNum': this.dataForm.classNum,
                'sort': this.dataForm.sort,
                'isStatus': this.dataForm.isStatus,
                'gmtCreate': this.dataForm.gmtCreate,
                'gmtModified': this.dataForm.gmtModified
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
